
//
//  GQProfileSettingController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/26.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQProfileSettingController.h"
#import "GQProfileAbout.h"
#import "GQProfileFeedBack.h"

@interface GQProfileSettingController () <UIAlertViewDelegate, UIActionSheetDelegate>

@property (strong, nonatomic) NSArray *groupCount;
@property (strong, nonatomic) NSMutableArray *groupText;

@end

@implementation GQProfileSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    self.tableView.bounces = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"distribute_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backToPre)];
}

- (void)backToPre
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.groupCount[section] integerValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"flag"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"flag"];
    }
    if (indexPath.section != 2 || indexPath.row != 0) {
        cell.textLabel.text = self.groupText[indexPath.section][indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        if (indexPath.row != 2 || indexPath.section != 1) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        if (indexPath.row == 2 && indexPath.section == 1) {
            cell.detailTextLabel.text = @"已是最新版本";
            cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
        }
        return cell;
    }else if(indexPath.section == 2 && indexPath.row == 0){
        UILabel *loginOutLabel = [[UILabel alloc] initWithFrame:cell.frame];
        loginOutLabel.text = self.groupText[indexPath.section][indexPath.row];
        loginOutLabel.font = [UIFont systemFontOfSize:15];
        loginOutLabel.textAlignment = NSTextAlignmentCenter;
        [cell addSubview:loginOutLabel];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 1) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"是否确定清除缓存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"清除", nil];
        alertView.delegate = self;
        [alertView show];
    }
    if (indexPath.section == 0 && indexPath.row == 0) {
        GQProfileAbout *GQPA = [[GQProfileAbout alloc] init];
        [self.navigationController pushViewController:GQPA animated:YES];
    }
    if (indexPath.section == 1 && indexPath.row == 0) {
        GQProfileFeedBack *GQPF = [[GQProfileFeedBack alloc] init];
        [self.navigationController pushViewController:GQPF animated:YES];
    }
    if (indexPath.section == 2 && indexPath.row == 0) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"退出后不会删除历史数据，下次登录依然可以使用本账号", @"退出登录", nil];
        [actionSheet showInView:self.view];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        NSMutableArray *arr = self.groupText[0];
        [arr replaceObjectAtIndex:1 withObject:@"清楚缓存"];
        [self.tableView reloadData];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%ld", buttonIndex);
}

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    int i = 0;
    for (UIView *subView in actionSheet.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)subView;
            [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            i++;
            if (i == 2) {
                [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:16];
            }
            if (i == 3) {
                btn.titleLabel.font = [UIFont systemFontOfSize:16];
            }
        }
    }
}

- (void)willPresentAlertView:(UIAlertView *)alertView
{
    alertView.frame = CGRectMake(0, 0, 100, 100);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 20;
    }
    return 1;
}

- (NSArray *)groupCount
{
    if (_groupCount == nil) {
        _groupCount = [NSMutableArray arrayWithObjects:@"2", @"3", @"1", nil];
    }
    return _groupCount;
}

- (NSMutableArray *)groupText
{
    if (_groupText == nil) {
        NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"关于古趣", @"清理缓存 (5.4MB)", nil];
        NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:@"意见反馈", @"给古趣打分", @"检查更新", nil];
        NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"退出登录", nil];
        _groupText = [NSMutableArray arrayWithObjects:arr1, arr2, arr3, nil];
    }
    return _groupText;
}

@end
